<template>
    <div>
        <!-- header -->
        <el-card class="!border-none" shadow="never">
            <el-alert
                type="warning"
                title="温馨提示：1.库存预警商品为库存不足的销售中商品；2.商品销售状态分为销售中且库存足够时才可下单购买"
                :closable="false"
                show-icon
            />
            <el-form ref="formRef" class="mb-[-16px] mt-[15px]" :model="queryParams" :inline="true">
                <el-form-item label="商品货号">
                    <el-input
                        class="ls-input"
                        v-model="queryParams.code"
                        placeholder="请输入商品货号"
                        clearable
                        @keyup.enter="resetPage"
                    />
                </el-form-item>
                <el-form-item label="商品名称">
                    <el-input
                        class="ls-input"
                        v-model="queryParams.name"
                        placeholder="请输入商品名称"
                        clearable
                        @keyup.enter="resetPage"
                    />
                </el-form-item>
<!--                <el-form-item label="商品分类">-->
<!--                    <el-tree-select-->
<!--                        :props="{ children: 'child_cats', label: 'name', value: 'id' }"-->
<!--                        class="flex-1"-->
<!--                        v-model="queryParams.cat_id"-->
<!--                        :data="optionsData.lists.list"-->
<!--                        clearable-->
<!--                        :default-expand-all="true"-->
<!--                        placeholder="请选择分类"-->
<!--                        check-strictly-->
<!--                    />-->
<!--                </el-form-item>-->
                <el-form-item>
                    <el-button type="primary" @click="resetPage">查询</el-button>
                    <el-button @click="resetParams">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>
        <el-card class="!border-none mt-4" shadow="never">
            <!-- 导航栏 -->
            <el-tabs v-model="selectOrm" class="demo-tabs" @tab-click="handleTabclick">
                <el-tab-pane :label="`商品列表`" name="" />
                <el-tab-pane :label="`已上架`" name="1" />
                <el-tab-pane :label="`已下架`" name="0" />
                <el-tab-pane :label="`已删除`" name="delete" />
                <div class="flex justify-end">
                    <router-link
                            :to="{
                        path: '/redPacket/addRedPacket',
                    }"
                    >
                        <el-button type="primary" class="w-[84px] h-[30px] mb-[9px] text-[12px]"
                        >新增商品</el-button
                        >
                    </router-link>
                    <!--                <el-button-->
                    <!--                    @click="uploadll"-->
                    <!--                    class="w-[84px] h-[30px] mb-[9px] text-[12px] ml-2"-->
                    <!--                    :disabled="ids.length > 0 ? false : true"-->
                    <!--                    >一键上架</el-button-->
                    <!--                >-->
                    <!--                <el-button-->
                    <!--                    @click="underall"-->
                    <!--                    class="w-[84px] h-[30px] mb-[9px] text-[12px] ml-2"-->
                    <!--                    :disabled="ids.length > 0 ? false : true"-->
                    <!--                    >一键下架</el-button-->
                    <!--                >-->
                    <el-button
                        type="danger"
                        @click="deleteall"
                        class="w-[84px] h-[30px] mb-[9px] text-[12px] ml-2"
                        :disabled="ids.length > 0 ? false : true"
                    >{{selectOrm=='delete'?'一键恢复':'一键删除'}}</el-button
                    >
                </div>
                <!-- 表格 -->
                <el-table
                    size="large"
                    :data="pager.lists"
                    @selection-change="handleSelectionChange"
                >
                    <el-table-column type="selection" width="60" />
                    <el-table-column label="商品编号" prop="code" min-width="200" />
                    <el-table-column label="商品信息" min-width="280">
                        <template #default="{ row }">
                            <div class="flex items-center">
                                <div>
									<el-image
									    style="width: 60px; height: 60px"
									    :src="row.cover"
									    :preview-src-list="[row.cover]"
									    :hide-on-click-modal="true"
									    :preview-teleported="true"
									    :fit="'cover'"
									>
									</el-image>
                                </div>
                                <el-tooltip :content="row.name" placement="top-start">
                                    <div class="ml-[10px] text_hidden">{{ row.name }}</div>
                                </el-tooltip>
                            </div>
                        </template>
                    </el-table-column>
                    <el-table-column label="价格" prop="sell_price" min-width="190" >
						<template #default="{ row }">
<!--                            <div>-->
<!--                                <span>商品市场价：</span>-->
<!--                                <span style="color: #2494f2">{{row.price_market}}</span>-->
<!--                            </div>-->
                            <div>
                                <span>商品售价：</span>
                                <span style="color: #2494f2">{{row.price_selling}}</span>
                            </div>
							<div>
							    <span>积分：</span>
							    <span style="color: #2494f2">{{row.price_market}}</span>
							</div>
							<div style="font-size: 12px; color: #2494f2">{{row.cateinfo.names[0]}}&nbsp;>&nbsp;{{row.cateinfo.names[1]}}</div>
						</template>
					</el-table-column>
                    <el-table-column label="库存" prop="stock" min-width="340" >
						<template #default="{ row }">
							<div>
                                <span>剩余库存：</span>
                                <span style="color: #2494f2">{{row.stock_total}}</span>
								<span>件，剩余库存：</span>
								<span style="color: #2494f2">{{row.stock_total-row.stock_sales}}</span>
								<span>件 ( 已销售 {{row.stock_sales}}件 )</span>
                            </div>
							<div>
                                <span>实际销售：</span>
                                <span style="color: #2494f2">{{row.stock_sales_y}}</span>
                                <span>&nbsp;件</span>
                            </div>
							<div>
                                <span>实际卖出：</span>
                                <span style="color: #2494f2">{{row.stock_sales_count}}</span>
                                <span>&nbsp;件</span>
                            </div>
						</template>
					</el-table-column>
<!--                    <el-table-column label="排序" prop="sort" min-width="80" />-->
                    <el-table-column label="销售状态" min-width="110">
                        <template #default="{ row }">
                            <el-tag
                                class="ml-2"
                                :type="['success', 'danger'][row.status]"
                                >{{['已下架', '销售中'][row.status]}}</el-tag
                            >
                        </template>
                    </el-table-column>
                    <el-table-column label="操作" min-width="160" fixed="right">
                        <template #default="{ row }">
                            <el-button type="primary" link>
                                <router-link
                                    :to="{
                                          path: '/redPacket/addRedPacket',
                                        query: {
                                            id: row.id,
                                            code: row.code,
                                        }
                                    }"
                                >
                                    编辑
                                </router-link>
                            </el-button>
                            <el-button
                                type="primary"
                                link
                                @click="handleunder(row.id, row.status)"
                                v-if="row.status == 1"
                            >
                                下架
                            </el-button>
                            <el-button
                                type="primary"
                                link
                                @click="handleunder(row.id, row.status)"
                                v-if="row.status == 0"
                            >
                                上架
                            </el-button>
                            <el-button
                                type="danger"
                                link
                                @click="handleDelete(row.id,row.deleted)"
                            >
							{{row.deleted==1?'恢复':'删除'}}
                            </el-button>
							<el-button
							    type="primary"
							    link
							    @click="addKu(row)"
							>
							    添加三方直播商品库
							</el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </el-tabs>
            <!-- 分页 -->
            <div class="flex justify-end mt-4">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
    </div>
</template>

<script lang="ts" setup>
import { usePaging } from '@/hooks/usePaging'
import { goodsRedDel,goodsRedList, goodsRedEdit,goodsRedRollbackDel } from '@/api/redPacket'
// import { idInfo, liveInfo } from "./data.ts";
import feedback from '@/utils/feedback'
import { getRoutePath } from '@/router'
import {addGoodsMz} from "@/api/live";

const selectOrm = ref('');

const queryParams = reactive({
    code: '',
    name: '',
    cat_id: [],
    status: '', // 【0：已下架|1：销售中】 不传默认全部
	type:'index', // 【index：普通商品列表|recycle：商品回收站列表】
});

const queryData = reactive({
    goods_id: '',
    name: '',
    pic:'',
    price: '',
    desc:'',
});

//分页处理
const { pager, getLists, resetPage, resetParams } = usePaging({
    fetchFun: goodsRedList,
    params: queryParams
});

getLists();


//删除商品
const ids = ref<any[]>([]);
const handleSelectionChange = (val: any[]) => {
    ids.value = val.map((item) => item.id);

};

const handleDelete = async (id: number, status: number) => {
    await feedback.confirm(status==0?'确定要删除？':'确定要恢复？');
	if(status==0){
		await goodsRedDel({ id_str: id });
	}else{
		await goodsRedRollbackDel({ id_str: id });
	}
    getLists()
};

//批量删除
const deleteall = async () => {
	let dataids = ''
	ids.value.map((item,index)=>{
		dataids = dataids+item+','
	})
	dataids = dataids.slice(0, -1)
    await feedback.confirm(selectOrm.value=="delete"?'确定要恢复?':'确定要删除？');
	if(selectOrm.value=="delete"){
		await goodsRedRollbackDel({ id_str: dataids });
	}else{
		await goodsRedDel({ id_str: dataids });
	}
    getLists()
};

//上下架商品
const handleunder = async (id: number, status: number) => {
    await feedback.confirm(`确定要${status == 0 ? '上架' : '下架'}`);
    await goodsRedEdit({ id: id, field_val: status == 0 ? 1 : 0 ,field_name:'status'});
    getLists()
};

//添加三方商品库
const addKu = async (val: { id: string; cover: string; name: string; price_selling: string }) => {
    queryData.goods_id = val.id
    queryData.pic = val.cover
    queryData.name = val.name
    queryData.price = val.price_selling
    queryData.desc = val.name
    await feedback.confirm('确定要到三方商品库？');
    await addGoodsMz(queryData);
    getLists()
};
//
// // 批量下架
// const underall = async () => {
//     await feedback.confirm('确定要下架？')
//     await statusGoods({ ids: ids.value, status: 0 })
//     getLists()
// };
//
// const uploadll = async () => {
//     await feedback.confirm('确定要上架？')
//     await statusGoods({ ids: ids.value, status: 1 })
//     getLists()
// };

//切换导航栏
const handleTabclick = async () => {
    await nextTick();
    if (selectOrm.value === 'delete') {
        queryParams.type = 'recycle';
        queryParams.status = '';
    } else {
        queryParams.type = 'index';
        queryParams.status = selectOrm.value;
    }
    getLists()
}
</script>

<style lang="scss" scope>
//TODO
.el-alert--warning.is-light {
    background-color: rgba(246, 248, 255, 1);
    color: rgba(64, 115, 250, 1);
}

.ls-input {
    width: 280px;
}
</style>
